package org.atlantis.grosbeak.lang.fruit;

import java.lang.reflect.Method;

import org.atlantis.grosbeak.lang.Exceptions;

/**
 * 通过无参数的工厂方法来实例化对象。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 * @param <T>
 */
public class DefaultFactoryMethodFruiting<T> implements Fruiting<T> {

	private Method method;

	/**
	 * @param method
	 *            无参数的工厂方法。
	 */
	public DefaultFactoryMethodFruiting(Method method) {
		this.method = method;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.atlantis.grosbeak.lang.fruit.Fruiting#fruit(java.lang.Object[])
	 */
	@SuppressWarnings("unchecked")
	public T fruit(Object[] args) {
		try {
			return (T) method.invoke(null);
		} catch (Exception e) {
			throw Exceptions
					.makeThrow(
							FruitingException.class,
							"Fail to instantiate with default factory method, because: '%s'.",
							e.getMessage());
		}
	}
}
